#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <vector>
#include <thread>
#include <mutex>
#include <sstream> // 用于将线程ID转换为字符串

using namespace std;

class task {
public:
    virtual int excute() = 0; // 纯虚函数
    virtual ~task() {}
};

class AddTask : public task {
public:
    AddTask() = default;
    AddTask(int a1, int b1) : a(a1), b(b1) {}
    int excute() override {
        cout << "Thread " << this_thread::get_id() << " - a+b= " << a + b << endl;
        return a + b;
    }

    ~AddTask() override {}

private:
    int a = 0;
    int b = 0;
};

class taskQueue {
public:
    bool addQueue(task* tmptask) {
        //lock_guard<std::mutex> lock(mutex);
        mutex.lock();

        queue.push_back(tmptask);
        mutex.unlock();
        //cout << "Producer Thread  " << this_thread::get_id() << "   "<<queue.size() << endl;
        return true;
    }

    task* outQueue() {
        /*lock_guard<std::mutex> lock(mutex);*/
        mutex.lock();
        if (queue.empty()) {
            mutex.unlock();
            return nullptr;
        }
        task* tmp = queue.back();
        queue.pop_back();
        mutex.unlock();
        return tmp;
    }

private:
    vector<task*> queue;
    mutex mutex;
};
/*
消费者线程在每次循环中都只尝试从队列中取出一个任务并执行，然后再次检查队列。
如果队列为空，消费者线程就会返回false并继续下一次循环。这意味着如果生产者线程没有及时生产任务，消费者线程就会空循环。
空循环导致 这个线程运行快，然后窗口就显示全是生产者了
*/
//class consumer {
//public:
//    bool xiaofei(taskQueue* queue) {
//        task* tmp = queue->outQueue();
//        if (tmp != nullptr) {
//            cout << "Consumer Thread " << this_thread::get_id() << " processing task." << endl;
//            tmp->excute();
//            delete tmp;
//            return true;
//        }
//        return false;
//    }
//};

class consumer {
public:
    void xiaofei(taskQueue* queue) {
        while (true) {
            task* tmp = queue->outQueue();
            if (tmp != nullptr) {
                cout << "Consumer Thread " << this_thread::get_id() << " processing task." << endl;
                tmp->excute();
                delete tmp;
            }
        }
    }
};

class producer {
public:
    bool shengchan(taskQueue* queue) {
        task* taskTmp = new AddTask(rand() % 100, rand() % 100);
        if (taskTmp != nullptr) {
            cout << "Producer Thread " << this_thread::get_id() << " producing task." << endl;
            queue->addQueue(taskTmp);
            return true;
        }
        return false;
    }
};

int main() {
    taskQueue* taskqueue = new taskQueue;
    cout << "Main Thread " << this_thread::get_id() << " starting threads." << endl;

    //如果 outQueue 在 addQueue 之前执行，并且队列为空，它将返回 nullptr，这可能导致消费者线程空循环
    thread t1([=]() {
        for (int i = 0; i < 100; i++) {
            producer produce;
            produce.shengchan(taskqueue);

        }
        });
    thread t2([=]() {
        for (int i = 0; i < 100; i++) {
            consumer consume;
            consume.xiaofei(taskqueue);
        }
        });

    t1.join();
    t2.join();
    delete taskqueue; // 释放任务队列对象
    cout << "Main Thread " << this_thread::get_id() << " finished." << endl;
    return 0;
}